<?php
defined('_JEXEC') or die('Restricted access');

$jversion = new JVersion();
if ($jversion->RELEASE == '1.5') {
  JToolBarHelper::title(JText::_('NS Pro Newsletters'), 'inbox.png');
}
else {
  JToolBarHelper::title(JText::_('NS Pro Newsletters'), 'writemess.png');
}

include('components/com_nspro/lib.php');

$db =& JFactory::getDBO();
$db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists`');
$lists = $db->loadObjectList();

?>
<script type="text/javascript"><!--

  function submitForm() {
    document.adminForm.submit();
  }

--></script>
<form id="adminForm" action="<?php echo JRoute::_( 'index.php' );?>" method="post" name="adminForm">
<div id="editcell">
    <table style="padding-bottom: 5px; width: 100%;">
    <tr>
    <td align="left">
    <!-- search newsletters in newer version -->
    </td>
    <td align="right"><?php

      $db->setQuery('SELECT `id`, `lname` FROM `#__nspro_lists`');
      $mlist = $db->loadObjectList();

      $first_element = array();
      $firstEl = new stdClass();
      $firstEl->id = -1;
      $firstEl->lname = JText::_('No List');
      $first_element[] = $firstEl;
      $mlist = array_merge($first_element, $mlist);
      $first_element = array();
      $firstEl = new stdClass();
      $firstEl->id = 0;
      $firstEl->lname = JText::_('All Lists');
      $first_element[] = $firstEl;
      $mlist = array_merge($first_element, $mlist);
      print JHTML::_('select.genericlist',  $mlist , 'nselect_list', 'onChange="submitForm();"', 'id', 'lname', $this->filter_nsls);

    ?></td></tr>
    </table>
    <table class="adminlist">
    <thead>
        <tr>
            <th width="5">
                <?php echo JHTML::_( 'grid.sort', JText::_('ID'), 'id', $this->olists['order_Dir'], $this->olists['order']); ?>
            </th>
            <th width="20">
                <input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count( $this->newsletters ); ?>);" />
            </th>
            <th class="edit">
                <?php echo JText::_('Edit'); ?>
            </th>
            <th class="subject">
                <?php echo JHTML::_( 'grid.sort', JText::_('Subject'), 'subject', $this->olists['order_Dir'], $this->olists['order']); ?>
            </th>
            <th class="title">
                <?php echo JHTML::_( 'grid.sort', JText::_('From Name'), 'fName', $this->olists['order_Dir'], $this->olists['order']); ?>
            </th>
            <th class="email">
                <?php echo JHTML::_( 'grid.sort', JText::_('From Email'), 'fEmail', $this->olists['order_Dir'], $this->olists['order']); ?>
            </th>
            <th align="center">
                <?php echo JHTML::_( 'grid.sort', JText::_('Sending Status'), 'sent', $this->olists['order_Dir'], $this->olists['order']); ?>
            </th>
            <th>
               <?php echo JText::_('Mailing Lists'); ?>
            </th>
            <th width="5" align="center">
              <img src="components/com_nspro/img/attachment-icon-fornspronewsletters.png" />
            </th>
            <th align="center">
                <?php echo JText::_( 'Sending Schedule' ); ?>
            </th>            
            <th align="center">
                <?php echo JText::_( 'Emails per Send' ); ?>
            </th>
            <th width="30%" class="message">
                <?php echo JText::_( 'Message' ); ?>
            </th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="14">
                <?php
                  print $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
    <tbody>
    <?php
    $k = 0;
    for ($i=0, $n=count( $this->newsletters ); $i < $n; $i++)
    {
        $row = &$this->newsletters[$i];

        $query = 'SELECT * FROM `#__nspro_newsletter_attachments` WHERE `newsletter_id` = ' . $row->id . ' ORDER BY `id`';
        $db->setQuery($query);
        $mattachments = $db->loadObjectList();

        $hasAttachment = false;
        if (count($mattachments) > 0) {
          $hasAttachment = true;
        }

        $mytotalsubs = GetSubCountByMailingLists($row->mailing_lists, 1, 0, 'id');

        $myleftsubs = GetSubCountByMailingLists($row->mailing_lists, 1, $row->LastID, 'id');

        $row->checked_out = false;
        $checked    = JHTML::_('grid.checkedout', $row, $i );
        $link       = JRoute::_( 'index.php?option=com_nspro&controller=nspro&task=nedit&cid[]='. $row->id );

        ?>
        <tr class="<?php echo "row$k"; ?>">
            <td>
                <?php echo $row->id; ?>
            </td>
            <td>
                <?php echo $checked; ?>
            </td>
            <td>
                <a href="<?php echo $link; ?>"><?php print JText::_('Edit'); ?></a>
            </td>
            <td>
                <?php echo $row->subject; ?>
            </td>
            <td>
                <?php echo $row->fName; ?>
            </td>
            <td>
                <?php echo $row->fEmail; ?>
            </td>
            <td align="center">
              <?php
              if ($row->sent) {
                print JText::_('Sent') . ' ' . JText::_('on') . ' ' . date('r', strtotime($row->datetime));
              }
              else {
                if ($row->LastID > 0) {
                  if ($mytotalsubs != 0) {
                    $spt = 100 - (($myleftsubs * 100) / $mytotalsubs);
                  }
                  else {
                    $spt = 0;
                  }
                  print JText::_('Sent up to ID:') . ' ' . $row->LastID;
                  print ' (';
                  printf("%01.2f", $spt);
                  print ' %)';

                  print '<div style="border: 1px solid #000; width: 60%; height: 20%;">';
                  print '<div style="background-color: #0f0; position: relative; left: -';
                  $nspt = 50 - ($spt / 2);
                  printf("%01.2f", $nspt);
                  print '%; height: 100%; width: ';
                  printf("%01.2f", $spt);
                  print '%;">' . "\n";
                  print ' </div></div>';

                }
                else {
                  print JText::_('Pending');
                }
              }
              ?>
            </td>
            <td align="center">
               <?php
                 $myLName = '';
                 $mySubLists = explode(',', $row->mailing_lists);
                 for ($j = 0; $j < count($lists); $j++) {
                   $arow = $lists[$j];
                   foreach ($mySubLists as $myListID) {
                     if ($arow->id == $myListID) {
                       if ($myLName == '') {
                         $myLName = $arow->lname;
                       }
                       else {
                         $myLName = $myLName . ', ' . $arow->lname;
                       }
                       break;
                     }
                   }
                 }

                 if ($myLName == '') {
                   $myLName = JText::_('No List Assigned');
                 }

                 print $myLName;

               ?>
            </td>
            <td align="center">
            <?php

              if ($hasAttachment) {
                ?><img src="components/com_nspro/img/attachment-icon-fornspronewsletters.png" /><?php
              }

            ?>
            </td>
            <td align="center">
              <?php
                if ($row->is_scheduled == '1') {
                  print JText::_('Scheduled to Send:') .'<br/>'.date('r', strtotime($row->scheduledatetime));
                }
                else {
                  print JText::_('Not Scheduled');
                }
              ?>
            </td>            
            <td align="center">
              <?php
                if ($row->emails_per_send == 0) {
                  print $row->emails_per_send . ' (' . JText::_('All') .')';
                }
                else {
                  print $row->emails_per_send;
                }
              ?>
            </td>
            <td>
                <?php
                  $msg = $row->message;
                  if (JString::strlen($msg) > 100) {
                    print htmlentities(JString::substr($msg, 0, 100) . '...');
                  }
                  else {
                    print htmlentities($msg);
                  }
                ?>
            </td>
        </tr>
        <?php
        $k = 1 - $k;
    }
    ?>
      </tbody>
    </table>
<br/>
<center><small><i>NS Pro by <a href="http://www.mavrosxristoforos.com/">Mavrosxristoforos.com</a> Copyright 2009-2011</i></small></center>
</div>

<input type="hidden" name="option" value="com_nspro" />
<input type="hidden" name="view" value="newsletters" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="boxchecked" value="0" />
<input type="hidden" name="controller" value="nspro" />
<input type="hidden" name="filter_order" value="<?php echo $this->olists['order']; ?>" />
<input type="hidden" name="filter_order_Dir" value="<?php echo $this->olists['order_Dir']; ?>" />
</form>